package beans;

import java.util.ArrayList;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import beans.interfaces.AgentDataLocal;
import beans.interfaces.TaskManagerLocal;
import agents.Agent;
import model.ACLMessage;

/**
 * Session Bean implementation class TaskManager
 */
@Stateless
@LocalBean
public class TaskManager implements TaskManagerLocal {

	@EJB
	private AgentDataLocal agentData;
	private ArrayList<Agent> agents;

    /**
     * Default constructor. 
     */
    public TaskManager() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public void executeOnAgents(ACLMessage aclMessage) {
		// TODO Auto-generated method stub
    	if(aclMessage==null)
    		return;
    	if(aclMessage.getReciever()==null) {
			agents=agentData.getAllAgents();
			for(Agent a : agents) {
				a.processMessage(aclMessage);
			}
    	}
    	else {
    		executeOnAgent(aclMessage);
    	}
	}

	@Override
	public void executeOnAgent(ACLMessage aclMessage) {
		// TODO Auto-generated method stub
		if(aclMessage==null)
    		return;
    	Agent a = agentData.getAgent(aclMessage.getReciever());
    	if(a!=null)
    		a.processMessage(aclMessage);
	}
}
